//import {login} from "@/pages/index/service/api";
import authorize from '@/common/authorize'
import {getOpenId} from '@/api/user.js'
import {storageKeyEnum, registerModelEnum, loginStateEnum} from '@/common/const.js'
// import {loginSms,register, getUserInfo, loginPassword} from '@/api/login/login.api.js'
// import {aesEncrypt} from '@/common/encryption.js'
	import {register, loginSms, getUserInfo} from '@/api/login.js'

const user = {
	state: {
		token: uni.getStorageSync(storageKeyEnum.accessToken),
		userInfo: uni.getStorageSync(storageKeyEnum.userInfo) ? JSON.parse(uni.getStorageSync(storageKeyEnum.userInfo)) : null,
		userInnerInfo: uni.getStorageSync(storageKeyEnum.userInnerInfo) ? JSON.parse(uni.getStorageSync(storageKeyEnum.userInnerInfo)) : null,
		openId: uni.getStorageSync(storageKeyEnum.openId),
		isLogin: uni.getStorageSync(storageKeyEnum.isLogin),
		invitedCode: uni.getStorageSync(storageKeyEnum.invitedCode),
		registerType: uni.getStorageSync(storageKeyEnum.registerType),
		userInnerIsLogin: uni.getStorageSync(storageKeyEnum.userInnerIsLogin),
	},
	
	mutations: {
		setToken: (state, token) => {
			state.token = token
			uni.setStorageSync(storageKeyEnum.accessToken,token)
		},
		setUserInfo: (state, userInfo) => {
			state.userInfo = userInfo
			let data = ''
			if(userInfo){
				data = JSON.stringify(userInfo)
			}
			uni.setStorageSync(storageKeyEnum.userInfo, data)
		},
		setUserInnerInfo: (state, userInnerInfo) => {
			state.userInnerInfo = userInnerInfo
			let data = ''
			if(userInnerInfo){
				data = JSON.stringify(userInnerInfo)
			}
			uni.setStorageSync(storageKeyEnum.userInnerInfo, data)
		},
		setOpenId: (state, openId) =>{
			state.openId = openId
			uni.setStorageSync(storageKeyEnum.openId,openId)
		},
		setIsLogin: (state, isLogin) =>{
			state.isLogin = isLogin
			uni.setStorageSync(storageKeyEnum.isLogin,isLogin)
		},
		setUserInnerIsLogin: (state, userInnerIsLogin) =>{
			state.userInnerIsLogin = userInnerIsLogin
			uni.setStorageSync(storageKeyEnum.userInnerIsLogin,userInnerIsLogin)
		},
		setInvitedCode: (state, invitedCode) =>{
			state.invitedCode = invitedCode
			uni.setStorageSync(storageKeyEnum.invitedCode,invitedCode)
		},
		setRegisterType: (state, registerType) =>{
			state.registerType = registerType
			uni.setStorageSync(storageKeyEnum.registerType,registerType)
		}
	},
	
	actions: {
		
		/**
		 * 获取openID
		 */
		getOpenId(context, params){			
			return new Promise((resolve, reject)=>{
				// #ifdef MP-WEIXIN || MP-ALIPAY
				// 获取code
				authorize.getAuthCode(code=>{
					// 获取openID
					getOpenId(code).then(res=>{
						// 保存openId
						if(params.isSave){
							context.commit('setOpenId', res.data)
						}
						resolve(res.data)
					})
				})
				// #endif
				
				// #ifdef H5
				resolve(true)
				// #endif
				
			})
		},
		
		/**
		 * 短信验证码登录
		 */
		loginSms(context, params) {
			return new Promise((resolve, reject)=>{
				const platform = context.getters.platform
				const invitedCode = context.getters.invitedCode
				const registerType = context.getters.registerType
				// 登录
				register({
					loginAccount: params.mobile,
					platform: platform,
					invitedCode: invitedCode,
					smsCode: params.smsCode,
					registerModel: registerModelEnum.SMS,
					registerType: registerType
				}).then(res=>{
					loginSms(params.mobile, params.smsCode).then(res => {
						context.commit('setToken', "Bearer " + res.access_token)
						// 获取用户信息
						getUserInfo().then(res=>{
							let data = res.data
							if(data.headerUrl){
								data.headerUrl = data.headerUrl
							}
							context.commit('setUserInfo', data)
							context.commit('setIsLogin', loginStateEnum.on)
							resolve(true)
						})
					})
				})
			})
		},
		
		/**
		 * 授权登录
		 */
		loginAuth(context, params) {
			return new Promise((resolve, reject)=>{
				const platform = context.getters.platform
				const invitedCode = context.getters.invitedCode
				const registerType = context.getters.registerType
				// 登录
				register({
					loginAccount: params.mobile,
					platform: platform,
					invitedCode: invitedCode,
					registerModel: registerModelEnum.WECHAT_AUTH,
					registerType: registerType
				}).then(res=>{
					loginSms(params.mobile, null).then(res => {
						context.commit('setToken', "Bearer " + res.access_token)
						// 获取用户信息
						getUserInfo().then(res=>{
							let data = res.data
							if(data.headerUrl){
								data.headerUrl = data.headerUrl
							}
							// 缓存用户信息
							context.commit('setUserInfo', data)
							context.commit('setIsLogin', loginStateEnum.on)
							resolve(true)
						})
					})
				})
			})
		},
		
		/**
		 * 更新用户信息
		 */
		updateUserInfo(context){
			return new Promise((resolve, reject)=>{
				getUserInfo().then(res=>{
					let data = res.data
					if(data.headerUrl){
						data.headerUrl = data.headerUrl
					}
					// 缓存用户信息
					context.commit('setUserInfo', data)
					resolve(true)
				})
			})
		},
		
		/**
		 * 用户退出
		 */
		logOut({commit}, params){
			return new Promise((resolve, reject) => {
				commit('setToken', '')
				commit('setUserInfo', '')
				commit('setIsLogin', '')
				resolve()
			})
		},
		
		changeLoginState(context){
			context.commit('setIsLogin', loginStateEnum.on)
		},
		
		/**
		 * 设置邀请码
		 */
		setInvitedCode({commit}, params){
			commit('setInvitedCode', params.invitedCode)
		},
		
		/**
		 * 设置邀请码
		 */
		setRegisterType({commit}, params){
			commit('setRegisterType', params.registerType)
		},
		
		/**
		 * 设置员工内部登录
		 */
		setUserInnerIsLogin({commit}, params){
			commit('setUserInnerInfo', params.userInnerInfo)
			commit('setUserInnerIsLogin', loginStateEnum.on)
		},
		
		/**
		 * 内部用户退出
		 */
		userInnerLogOut({commit}, params){
			return new Promise((resolve, reject) => {
				commit('setUserInnerInfo', '')
				commit('setUserInnerIsLogin', '')
				resolve()
			})
		},
		
		
	}
}

export default user
